﻿using ApplicationModels.A04.Interactions;
using Commons.Requests;
using Commons.Responses;
using EntityModels.E04.Interactions;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class StarController : BaseController<int, Star, StarAM>
    {
        public StarController(IBaseApplicationService<int, Star, StarAM> apiService) : base(apiService)
        {
        }

        public override async Task<DatasResponse<StarAM>> GetBySelectAsync([FromQuery] PagingRequest req)
        {
            if (string.IsNullOrWhiteSpace(req.ExtendedValue1) || req.ExtendedValue1 == AppUserId)
            {
                return await ApiService.GetAllAsync(x => x.AppUserId == Guid.Parse(AppUserId));
            }
            Guid userId = Guid.Parse(AppUserId);
            Guid.TryParse(req.ExtendedValue1, out userId);
            return await ApiService.GetAllAsync( x => x.IsPublicly == true&&x.AppUserId == userId);
        }
        public override Task<MessageResponse> AddAsync(StarAM apiEntity)
        {
            apiEntity.AppUserId = AppUserId;
            return base.AddAsync(apiEntity);
        }

        public override async Task<MessageResponse> UpdateAsync(StarAM apiEntity)
        {
            var res = await ApiService.GetAsync(x => x.AppUserId == Guid.Parse(AppUserId) && x.Id == apiEntity.Id);
            if (res.Data == null)
            {
                return new MessageResponse("你在搞什么?", false);
            }
            apiEntity.AppUserId = AppUserId;
            return await base.UpdateAsync(apiEntity);
        }
    }
}
